package cn.baddonkey.ui;

import cn.baddonkey.settings.Settings;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

/**
 * @author junheng.zhou
 * @date 2022-07-04
 */
public class MainSetting implements Configurable {
    private JTextField akTextField;
    private JTextField skTextField;
    private JPanel mainPanel;

    private GridBagLayout gridBagLayout = new GridBagLayout();
    private GridBagConstraints constraints = new GridBagConstraints();

    private Settings settings = Settings.getInstance();

    public MainSetting() {
        init();
    }

    private void init() {
        this.createUIForm();
        this.settings = Settings.getInstance();
        akTextField.setText(settings.getSettingMap().get("accessKey"));
        skTextField.setText(settings.getSettingMap().get("secretKey"));
    }

    @Nls(capitalization = Nls.Capitalization.Title)
    @Override
    public String getDisplayName() {
        return "Variable Name Helper Setting";
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        return mainPanel;
    }

    /**
     * 设置apply按钮是否可用，数据修改时被调用
     *
     * @return
     */
    @Override
    public boolean isModified() {
        String accessKey = settings.getSettingMap().get("accessKey");
        String secretKey = settings.getSettingMap().get("secretKey");
        String newAccessKey = akTextField.getText();
        String newSecretKey = skTextField.getText();
        return !StringUtils.equals(accessKey, newAccessKey) || !StringUtils.equals(secretKey, newSecretKey);
    }

    /**
     * 点击apply按钮后被调用
     *
     * @throws ConfigurationException
     */
    @Override
    public void apply() throws ConfigurationException {
        String newAccessKey = akTextField.getText();
        String newSecretKey = skTextField.getText();
        settings.getSettingMap().put("accessKey", newAccessKey);
        settings.getSettingMap().put("secretKey", newSecretKey);
//        settings.loadState(settings);
    }

    /**
     * reset按钮被点击时触发
     */
    @Override
    public void reset() {
        akTextField.setText(settings.getSettingMap().get("accessKey"));
        skTextField.setText(settings.getSettingMap().get("secretKey"));
    }

    private void createUIForm() {
        mainPanel = new JPanel();
        mainPanel.setLayout(gridBagLayout);

        JLabel akLabel = new JLabel("APP ID：");
        JLabel skLabel = new JLabel("密钥：");
        JLabel tipsLabel = new JLabel("此处填写您的百度翻译API秘钥信息，插件不会收集您的秘钥信息，代码完全开源：https://gitee.com/zjhsda/var-name-helper-plugin");
        akTextField = new JTextField();
        skTextField = new JTextField();

        constraints.fill = GridBagConstraints.BOTH;

        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 1;
        gridBagLayout.setConstraints(akLabel, constraints);
        mainPanel.add(akLabel);

        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.gridwidth = 1;
        gridBagLayout.setConstraints(akTextField, constraints);
        mainPanel.add(akTextField);

        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.gridwidth = 1;
        gridBagLayout.setConstraints(skLabel, constraints);
        mainPanel.add(skLabel);

        constraints.gridx = 1;
        constraints.gridy = 1;
        constraints.gridwidth = 1;
        gridBagLayout.setConstraints(skTextField, constraints);
        mainPanel.add(skTextField);

        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = 2;
        gridBagLayout.setConstraints(tipsLabel, constraints);
        mainPanel.add(tipsLabel);
    }
}

